#!/usr/bin/php
<?
set_include_path("../include");
require("header.inc.php");

$rows = Zotero_DB::query("SELECT shardID, shardHostID FROM shards ORDER BY shardID");
foreach ($rows as $row) {
	echo "Shard: " . $row['shardID'] . "\n";
	
	Zotero_DB::query("UPDATE itemNotes SET noteSanitized='' WHERE note='' AND noteSanitized IS NULL", false, $row['shardID']);
	
	while ($notes = Zotero_DB::query("SELECT itemID, note FROM itemNotes WHERE noteSanitized IS NULL LIMIT 500", false, $row['shardID'])) {
		echo "Processing " . sizeOf($notes) . " items\n";
		
		foreach ($notes as $note) {
			$noteTextSanitized = Zotero_Notes::sanitize($note['note']);
			$noteIsSanitized = $note['note'] == $noteTextSanitized;
			
			$title = Zotero_Notes::noteToTitle($noteTextSanitized);
			$noteTextSanitized = $noteIsSanitized ? '' : $noteTextSanitized;
			
			$sql = "UPDATE itemNotes SET noteSanitized=?, title=? WHERE itemID=?";
			Zotero_DB::query($sql, array($noteTextSanitized, $title, $note['itemID']), $row['shardID']);
		}
	}
	echo "Done\n";
	sleep(1);
}
?>
